package com.dingding.service.user.core.dao.impl;

import java.util.Date;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Vehicleinfo;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.user.dto.SyncDriverAuthDTO;
import com.dingding.service.user.core.dao.IVehicleInfoDao;

@Component("vehicleInfoDao")
public class VehicleInfoDaoImpl extends BaseDaoImpl<Vehicleinfo> implements IVehicleInfoDao {

	/**
	 * 
	 * @Description: 获取用户的认证信息
	 * @param loginId 用户登录id
	 * @author fengshuonan
	 * @return SyncDriverAuthDTO 车辆认证信息
	 * 
	 */
	@Override
	public SyncDriverAuthDTO getDriverAuthInfo(String loginId) {

		Vehicleinfo vehicleinfo = null;

		Criterion criterion = Restrictions.eq("loginId", loginId);
		try {
			vehicleinfo = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("获取用户认证信息时出错！VehicleInfoDaoImpl：getDriverAuthInfo()", e);
		}

		if (vehicleinfo == null) {
			log.info("用户不存在！VehicleInfoDaoImpl:getDriverAuthInfo()");
			return null;
		}

		SyncDriverAuthDTO syncDriverAuthDTO = new SyncDriverAuthDTO();

		BeanUtils.copyProperties(vehicleinfo, syncDriverAuthDTO);

		// 拼接图片地址
		syncDriverAuthDTO.setXszImage(ToolUtil.imageAddress(syncDriverAuthDTO.getXszImage()));
		syncDriverAuthDTO.setVehicleImage(ToolUtil.imageAddress(syncDriverAuthDTO.getVehicleImage()));
		syncDriverAuthDTO.setJszImage(ToolUtil.imageAddress(syncDriverAuthDTO.getJszImage()));

		return syncDriverAuthDTO;
	}

	
	/**
	 * @Title: saveVehicleInfoByNum 
	 * @Description: 保存用户车辆信息
	 * @param loginId 用户ID
	 * @author CHEHAUNBO
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void addVehicleInfo(String loginId) {

		Vehicleinfo vehicleinfo = new Vehicleinfo();
		// viid
		vehicleinfo.setUvId(ToolUtil.getUid());
		// loginId
		vehicleinfo.setLoginId(loginId);
		// 认证时间
		vehicleinfo.setCreateTime(new Date());
		// 认证状态 0:保留
		vehicleinfo.setVstatus(0);
		// 是否认证
		vehicleinfo.setIsAuth(1);
		//车牌号
		vehicleinfo.setLisencePlate("");
		vehicleinfo.setVehicleModel("");
		vehicleinfo.setVehicleType("");
		vehicleinfo.setVehicleColor("");
		vehicleinfo.setXszImage("");
		vehicleinfo.setJszImage("");
		vehicleinfo.setVehicleImage("");

		try {
			super.save(vehicleinfo);
		} catch (Exception e) {
			log.error("添加用户车辆信息！", e);
		}
		
	}
	
	

}
